package com.wyr.admin.mq.listener;

import com.rabbitmq.client.Channel;
import com.wyr.admin.service.UserInfoService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;

import java.nio.charset.StandardCharsets;

/**
 * 动态队列队列监听
 * <p>
 * 注意：此动态队列在集群部署时，本实例创建的动态队列，本实例消费，不会集群后随机消费
 *
 * @author wyr
 * @version 1.0
 * &#064;title DynamicQueueListener
 * &#064;description
 * &#064;date 2025/04/01 17:05
 */
@Slf4j
@Setter
public class DynamicQueueListener implements ChannelAwareMessageListener {

    private UserInfoService userInfoService;

    @Override
    public void onMessage(Message amqpMessage, Channel channel) {
        MessageProperties messageProperties = amqpMessage.getMessageProperties();
        long startTime = System.currentTimeMillis();
        try {
            String message = new String(amqpMessage.getBody(), StandardCharsets.UTF_8);
            log.info("{} tts语音合成队列接收到消息：{}", messageProperties.getConsumerQueue(), message);
            channel.basicAck(messageProperties.getDeliveryTag(), false);
            log.info("tts语音合成队列处理结束{}", messageProperties.getConsumerQueue());
        } catch (Exception e) {
            log.error("tts语音合成队列异常{}", e.getMessage(), e);
        } finally {
            long sumTime = System.currentTimeMillis() - startTime;
            log.info("mq结束,耗时：{}ms", sumTime);
        }
    }

}
